package com.qbgs.util;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.wutka.jox.JOXBeanInputStream;
import com.wutka.jox.JOXBeanOutputStream;


/**
 * XML与Object之间的互相转换工具类
 * @author lyh
 * @version 2011-11-01
 * @see JoxUtil
 * @since
 */
public class JoxUtil
{
    /***
     * 日志
     */
    private static final Logger logger = Logger.getLogger(JoxUtil.class);

    /**
     * 
     * Description: 得到类的名称 ，比如com.hafx.codesure.center.JOX,得到的就是JOX
     * @param bean
     * @return String
     * @see
     */
    private static String beanName(Object bean)
    {
        String fullClassName = bean.getClass().getName();
        String classNameTemp = fullClassName.substring(fullClassName.lastIndexOf(".") + 1,
            fullClassName.length());
        return classNameTemp;
    }

    /**
     * 
     * Description: 得到类的名称 ，比如com.hafx.codesure.center.JOX,得到的就是JOX
     * @param fullClassName
     * @return String
     * @see
     */
    private static String beanName(String fullClassName)
    {
        String classNameTemp = fullClassName.substring(fullClassName.lastIndexOf(".") + 1,
            fullClassName.length());
        return classNameTemp;
    }

    /**
     * 
     * Description: 将一个类转换成XML
     * @param bean
     * @return String
     * @see
     */
    public static String beanToXML(Object bean)
    {
        OutputStream xmlData = new ByteArrayOutputStream();
        JOXBeanOutputStream joxOut = new JOXBeanOutputStream(xmlData,"UTF-8");
        
        try
        {
            joxOut.writeObject(beanName(bean), bean);
            return xmlData.toString();
        }
        catch (IOException e)
        {
            logger.error("IOException", e);
        }
        finally
        {
            try
            {
                xmlData.close();
                joxOut.close();
            }
            catch (IOException e)
            {
                logger.error("IOException", e);
            }
        }
        return null;
    }

    /**
     * 
     * Description: 将XML转换成实体类
     * @param xml
     * @param className
     * @return 
     * @see
     */
    @SuppressWarnings("rawtypes")
    public static Object xmlToBean(String xml, Class className)
    {
        JOXBeanInputStream joxIn = null;
        try
        {
            ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
            joxIn = new JOXBeanInputStream(is);
            Object o = (Object)joxIn.readObject(className);
            return o;
        }
        catch (IOException e)
        {
            logger.error(e);
        }
        finally
        {
            try
            {
                IOUtils.closeQuietly(joxIn);
            }
            catch (Exception e)
            {
                logger.error("IOException", e);
            }
        }
        return null;
    }

    /**
     * 
     * Description: 将一个类数组转换成XML
     * 
     * @param beans
     * @param className
     * @return String
     * @see
     */
    @SuppressWarnings("rawtypes")
    public static String beansToXML(Object[] beans, Class className)
    {
        if (beans == null || beans.length == 0)
        {
            return null;
        }
        XStream xstream = new XStream(new DomDriver());
        xstream.alias(beanName(className.toString()), className);
        String xml = xstream.toXML(beans);
        xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xml;
        return xml;
    }

    /**
     * 
     * Description: 将一个类数组转换成XML
     * 
     * @param beans
     * @param className
     * @return String
     * @see
     */
    @SuppressWarnings("rawtypes")
    public static String beansToXML(List list, Class className)
    {
        if (list == null || list.size() == 0)
        {
            return null;
        }
        XStream xstream = new XStream(new DomDriver());
        xstream.alias(beanName(className.toString()), className);
        String xml = xstream.toXML(list);
        xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xml;
        return xml;
    }

    /**
     * 
     * Description: 将一个xml转换成实体类数组
     * 
     * @param xml
     * @param className
     * @return Object
     * @see
     */
    @SuppressWarnings("rawtypes")
    public static Object xmlToBeans(String xml, Class className)
    {
        XStream xstream = new XStream(new DomDriver());
        xstream.alias(beanName(className.toString()), className);
        return xstream.fromXML(xml);
    }

}
